package com.studentmanager.service.Impl;

import com.studentmanager.domain.TeachingCalling;
import com.studentmanager.domain.TeachingCallingDetail;
import com.studentmanager.mapper.TeachingCallingDetailMapper;
import com.studentmanager.mapper.TeachingCallingMapper;
import com.studentmanager.service.TeachingCallingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TeachingCallingServiceimpl implements TeachingCallingService {

    @Autowired
    private TeachingCallingMapper teachingCallingMapper;

    @Autowired
    private TeachingCallingDetailMapper teachingCallingDetailMapper;
    // 发布点名
    @Override
    public void createTeachingCalling(int classId, int teacherId, Date startTime, Date endTime) {
        TeachingCalling calling = new TeachingCalling();
        calling.setClassId(classId);
        calling.setCreator(teacherId);
        calling.setStartTime(startTime);
        calling.setEndTime(endTime);
        calling.setEnded("0"); // 点名进行中
        teachingCallingMapper.createCalling(calling);

        // 生成该班级所有学生的点名详情记录
        List<Integer> studentIds = (List<Integer>) getStudentsByClass(classId); // 假设获取学生的ID列表
        for (Integer studentId : studentIds) {
            TeachingCallingDetail detail = new TeachingCallingDetail();
            detail.setStuInClassId(studentId);
            detail.setCallId(calling.getId());
            teachingCallingDetailMapper.insertCallingDetail(detail);
        }
    }
@Override
    // 学生签到
    public void studentSignIn(int stuInClassId, int callId, Date answerTime, String answerResult, String noAnswerReason) {
    TeachingCallingDetail detail = teachingCallingDetailMapper.findByStuInClassIdAndCallId(stuInClassId, callId);
    if (detail != null) {
        detail.setAnswerTime(answerTime);
        detail.setAnswerResult(answerResult);
        detail.setNoAnswerReason(noAnswerReason);
        teachingCallingDetailMapper.update(detail);
    }
    }

    // 获取学生ID列表（根据班级ID，实际实现可以从学生表中查询）
    private TeachingCalling getStudentsByClass(int classId) {
        // TODO: 查询数据库返回学生ID列表
        return teachingCallingMapper.findById(classId);
    }

    // 更新点名状态
    public void updateCallingStatus(int callId, String ended) {
        teachingCallingMapper.updateCallingStatus(callId, ended);
    }
}
